<?php
namespace catchAdmin\disease\controller;

use app\common\model\DiseaseType as DisTypeModel;
use catcher\base\CatchController;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
use app\common\model\Disease as DiseaseModel;
use catcher\exceptions\FailedException;

class Illness extends CatchController
{
    protected $disease;

    public function __construct(DiseaseModel $disease)
    {
        $this->disease = $disease;
    }

    /**
     * 列表
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index(CatchRequest $request): \think\response\Json
    {
        return CatchResponse::paginate($this->disease->getLists($request->param()));
    }

    /**
     * 保存
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function save(CatchRequest $request): \think\response\Json
    {
        return CatchResponse::success($this->disease->addData($request->param()));
    }

    /**
     * 更新
     *
     * @time 2020年01月09日
     * @param $id
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function update($id, CatchRequest $request): \think\response\Json
    {
        return CatchResponse::success($this->disease->saveData($id, $request->param()));
    }

    /**
     * 删除
     *
     * @time 2020年01月09日
     * @param $id
     * @return \think\response\Json
     */
    public function delete($id): \think\response\Json
    {
        return CatchResponse::success($this->disease->destroy($id));
    }

    /**
     * 获取病种数据
     * @return \think\response\Json
     */
    public function getdisease(): \think\response\Json
    {
        return CatchResponse::success($this->disease->getRows());
    }

    /**
     * 模糊查询疾病
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function getDiseaseData(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        $where = [];
        if (!isset($req['name']) || empty($req['name'])) {
            throw new FailedException("请输入疾病名称");
        }
        $where[] = ['name', '=', ['$regex' => '^'.$req['name']]];
        $ret = $this->disease->where($where)->field('name,illnessno')->select();
        return CatchResponse::success($ret);
    }
}